/**
 * 
 */
package cn.gov.msa.collection;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import cn.gov.msa.model.Org;
import cn.gov.msa.model.Role;
import cn.gov.msa.model.UserDetail;
import cn.gov.msa.util.PropertiesTools;
import cn.gov.msa.util.WebServiceTools;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * @author Administrator
 *
 */
public class LdapService {
	
	private static Logger logger = Logger.getLogger(LdapService.class);

	private static String url = PropertiesTools.readProperties("ldap.url");
	
	public static String queryUser(JSONObject param) throws Exception {
		JSONObject result = new JSONObject();
		
		if(param.has("username") && !"".equals(param.getString("username"))){
			param.put("userName", param.getString("username"));
		} else {
			result.put("total", "0");
			result.put("rows", new JSONArray());
			return result.toString();
		}
		if(param.has("orgcode") && !"".equals(param.getString("orgcode")))
			param.put("orgnazations", param.getString("orgcode"));
		
		
        String[] parameter = new String[]{param.toString()};      
		
    	JSONObject jsonResult = JSONObject.fromObject(WebServiceTools.getDatasByUrl(url, "UserQueryServices_userQueryByCondition", parameter));
    	if(jsonResult.getInt("result") == 1){
    		int total = jsonResult.getJSONObject("datas").getJSONObject("table").getJSONObject("row").getInt("total");
    		
    		JSONArray rowsTemp = jsonResult.getJSONObject("datas").getJSONObject("table").getJSONObject("row").getJSONObject("table").getJSONArray("row");
    		JSONArray rows = new JSONArray();
    		for (Object object : rowsTemp) {
				JSONObject row = JSONObject.fromObject(object);
				
				JSONObject rownew = new JSONObject();
				rownew.put("userid", row.getString("userId"));
				rownew.put("username", row.getString("userName"));
				rownew.put("orgcode", row.getString("maritimeOrgCode"));
				if(row.has("orgName"))
					rownew.put("orgname", row.getString("orgName"));
				if(row.has("deptCode"))
					rownew.put("deptcode", row.getString("deptCode"));
				if(row.has("deptName"))
					rownew.put("deptname", row.getString("deptName"));

				
				rows.add(rownew);
			}
    		
    		result.put("total", total);
    		result.put("rows", rows);
    	}else {
    		result.put("total", "0");
    		result.put("rows", new JSONArray());
    	}
    	
		return result.toString();
	}
	
	public static void main(String[] args) throws Exception {
		String result = LdapService.queryUser(JSONObject.fromObject("{'orgcode':'0601'}"));
		
		System.out.println(result);
		
		
//		
//		JSONObject jsonParam = new JSONObject();
//		jsonParam.put("orgName", "盐城东台海事处");
//		
//
//        String[] parameter = new String[]{jsonParam.toString()};   
//        
//    	JSONObject jsonResult = JSONObject.fromObject(WebServiceTools.getDatasByUrl(url, "OrgQueryServices_queryOrgByCondition", parameter));
//
//    	System.out.println(jsonResult);
	}
	
	public LdapService(){}
	
	public JSONArray getUserRole(String userid) {
		JSONArray jaUserRole = new JSONArray();
		return jaUserRole;
	}
	

	public static JSONArray getUserByRoleCode(String rolecode,String orgcode) throws Exception {

		JSONArray jaUser = new JSONArray();
		JSONObject param = new JSONObject();
		param.put("pageSize", 10000);
		param.put("roleCode", rolecode);
		if(!"".equals(orgcode))
			param.put("maritimeOrgCode", orgcode);
		String[] parameter = new String[]{param.toString()};      
		JSONObject result = JSONObject.fromObject(WebServiceTools.getDatasByUrl(url, "RoleQueryServices_roleQueryUsersList", parameter));
		if(result.getInt("result") == 1){
			JSONArray row = result.getJSONObject("datas").getJSONObject("table").getJSONObject("row").getJSONObject("table").getJSONArray("row");
			if(row.size()>0) {
				for (int i = 0; i < row.size(); i++) {
					JSONObject user = row.getJSONObject(i);
					JSONObject userDetail = new JSONObject();
					userDetail.put("userid", user.optString("userId"));
					userDetail.put("username", user.optString("userName"));
					userDetail.put("orgcode", user.optString("orgCode"));
					userDetail.put("orgname", user.optString("orgName"));
					jaUser.add(userDetail);
				}
			}
			
		}
		return jaUser;
	}
	
	public JSONObject getUserByUserId(String userid) {
		JSONObject jsonUserResult = new JSONObject();
		
		return jsonUserResult;
	}

	public static UserDetail getUserDetail(String userid, Integer isrole) throws Exception{

		ApplicationContext ctx = null;
		UserDetail userDetail = new UserDetail();
		
		JSONObject param = new JSONObject();
		
		param.put("userId", userid);
		if(1==isrole) {
			param.put("isQueryRoles", "1");
		}
        String[] parameter = new String[]{param.toString()};      
    	JSONObject jsonResult = JSONObject.fromObject(WebServiceTools.getDatasByUrl(url, "UserQueryServices_userQuery4Detail", parameter));
    	
    	
        //王鑫
//    	JSONObject jsonResult = JSONObject.fromObject("{\"result\":\"1\",\"info\":\"查询成功\",\"datas\":{\"table\":{\"tableName\":\"user\",\"row\":{\"city\":\"南京市\",\"cn\":\"王鑫\",\"country\":\"中国\",\"credentialsType\":\"身份证\",\"departments\":\"06010040\",\"dutyGrade\":\"科员\",\"email\":\"wangxin006@msa.gov.cn\",\"employeeType\":\"正式用户\",\"gender\":\"女\",\"givenName\":\"王鑫\",\"idNumber\":\"370725198510220229\",\"ismanager\":\"否\",\"mail\":\"wangxin006@msa.gov.cn\",\"mobile\":\"17751000840\",\"orderNum\":\"-1\",\"orgName\":\"南京海事局\",\"organizations\":\"0601\",\"province\":\"江苏省\",\"sn\":\"王鑫\",\"uid\":\"370725198510220229\",\"userName\":\"王鑫\",\"userStatus\":\"Active\",\"userType\":\"正式用户\",\"workType\":\"公务员编制\",\"table\":[{\"tableName\":\"role\",\"row\":[{\"businessType\":\"行政综合\",\"roleCode\":\"CMP_99_R00001\",\"productRoleId\":\"1\",\"roleName\":\"门户基础角色\",\"appId\":\"99\",\"appName\":\"公用\",\"systemHeight\":\"1000\",\"systemWidth\":\"1200\",\"isUpdate\":\"否\",\"isDefaultRole\":\"是\",\"roleType\":\"通用角色\",\"accessPlatform\":\"p\",\"suitRange\":\"全国\"},{\"businessType\":\"系统管理\",\"roleCode\":\"CMP_006_R00008\",\"productRoleId\":\"8\",\"roleName\":\"默认角色\",\"appId\":\"006\",\"appName\":\"运维系统\",\"url\":\"http://cmp.msa.gov.cn/cas\",\"systemHeight\":\"1000\",\"systemWidth\":\"1200\",\"isUpdate\":\"否\",\"isDefaultRole\":\"是\",\"roleType\":\"默认角色\",\"logoutUrl\":\"http://198.10.9.207/cas/idmlogout\",\"accessPlatform\":\"p\",\"suitRange\":\"全国\",\"isBlank\":\"否\"},{\"businessType\":\"行政综合\",\"roleCode\":\"CMP_010_R00004\",\"productRoleId\":\"4\",\"roleName\":\"公告发布者\",\"appId\":\"010\",\"appName\":\"统一内容发布系统\",\"url\":\"http://cmp.msa.gov.cn/URCms/jeeadmin/jeecms/index.do\",\"systemHeight\":\"1000\",\"systemWidth\":\"1200\",\"isUpdate\":\"否\",\"isDefaultRole\":\"是\",\"roleType\":\"默认角色\",\"logoutUrl\":\"http://cmp.msa.gov.cn/URCms/jeeadmin/jeecms/logout.do?returnUrl=index.do\",\"accessPlatform\":\"p\",\"suitRange\":\"全国\",\"isBlank\":\"否\"},{\"businessType\":\"行政综合\",\"roleCode\":\"CMP_022_R00001\",\"productRoleId\":\"CMP_022_R00001\",\"roleName\":\"内网邮件\",\"appId\":\"022\",\"appName\":\"内网邮件\",\"url\":\"http://cmp.msa.gov.cn/coremail/sso.jsp?to=webmail\",\"systemHeight\":\"1000\",\"systemWidth\":\"1200\",\"isUpdate\":\"是\",\"isDefaultRole\":\"是\",\"roleType\":\"默认角色\",\"accessPlatform\":\"p\",\"suitRange\":\"全国\",\"isBlank\":\"是\"},{\"businessType\":\"船员管理\",\"roleCode\":\"CMP_019_R00007\",\"productRoleId\":\"10000000007\",\"roleName\":\"各局其他证书审批\",\"appId\":\"019\",\"appName\":\"海船船员管理\",\"url\":\"http://cmp.msa.gov.cn/cybs_web/ssoLoginAction\",\"systemHeight\":\"1000\",\"systemWidth\":\"1200\",\"isUpdate\":\"否\",\"isDefaultRole\":\"否\",\"roleType\":\"一般角色\",\"logoutUrl\":\"http://cmp.msa.gov.cn/cybs_web/rbac/logoutAction.do?logout=1\",\"accessPlatform\":\"p\",\"suitRange\":\"全国\",\"isBlank\":\"否\"},{\"businessType\":\"船员管理\",\"roleCode\":\"CMP_019_R00030\",\"productRoleId\":\"10000000021\",\"roleName\":\"各局船员注册审批\",\"appId\":\"019\",\"appName\":\"海船船员管理\",\"url\":\"http://cmp.msa.gov.cn/cybs_web/ssoLoginAction\",\"systemHeight\":\"1000\",\"systemWidth\":\"1200\",\"isUpdate\":\"否\",\"isDefaultRole\":\"否\",\"roleType\":\"一般角色\",\"logoutUrl\":\"http://cmp.msa.gov.cn/cybs_web/rbac/logoutAction.do?logout=1\",\"accessPlatform\":\"p\",\"suitRange\":\"全国\",\"isBlank\":\"否\"},{\"businessType\":\"船员管理\",\"roleCode\":\"CMP_019_R00033\",\"productRoleId\":\"10000000024\",\"roleName\":\"各局培训合格证审批\",\"appId\":\"019\",\"appName\":\"海船船员管理\",\"url\":\"http://cmp.msa.gov.cn/cybs_web/ssoLoginAction\",\"systemHeight\":\"1000\",\"systemWidth\":\"1200\",\"isUpdate\":\"否\",\"isDefaultRole\":\"否\",\"roleType\":\"一般角色\",\"logoutUrl\":\"http://cmp.msa.gov.cn/cybs_web/rbac/logoutAction.do?logout=1\",\"accessPlatform\":\"p\",\"suitRange\":\"全国\",\"isBlank\":\"否\"},{\"businessType\":\"系统管理\",\"roleCode\":\"CMP_035_R000002\",\"productRoleId\":\"CMP_035_R000002\",\"roleName\":\"MDM系统用户\",\"appId\":\"035\",\"appName\":\"移动安全管理系统\",\"url\":\"http://cmp.msa.gov.cn/webservice/haishiju/webLogin.php\",\"systemHeight\":\"1000\",\"systemWidth\":\"1200\",\"isUpdate\":\"否\",\"isDefaultRole\":\"是\",\"roleType\":\"默认角色\",\"accessPlatform\":\"p\",\"suitRange\":\"全国\",\"isBlank\":\"是\"},{\"businessType\":\"系统管理\",\"roleCode\":\"CMP_001_R00010\",\"productRoleId\":\"1234\",\"roleName\":\"部门审核人\",\"appId\":\"001\",\"appName\":\"4A系统\",\"url\":\"http://cmp.msa.gov.cn\",\"systemHeight\":\"1000\",\"systemWidth\":\"1200\",\"isUpdate\":\"是\",\"isDefaultRole\":\"否\",\"roleType\":\"通用角色\",\"accessPlatform\":\"p\",\"suitRange\":\"全国\",\"isBlank\":\"否\"},{\"businessType\":\"船员管理\",\"roleCode\":\"CMP_047_R00001\",\"productRoleId\":\"CMP_047_R00001\",\"roleName\":\"一般成员\",\"appId\":\"047\",\"appName\":\"船员管理系统\",\"url\":\"http://cmp.msa.gov.cn/crew_mgt/pub/ssoLogin.action\",\"systemHeight\":\"1000\",\"systemWidth\":\"1200\",\"isUpdate\":\"是\",\"isDefaultRole\":\"否\",\"roleType\":\"一般角色\",\"accessPlatform\":\"p\",\"suitRange\":\"全国\",\"isBlank\":\"否\"},{\"businessType\":\"船员管理\",\"roleCode\":\"CMP_047_R00004\",\"productRoleId\":\"CMP_047_R00004\",\"roleName\":\"服务业务\",\"appId\":\"047\",\"appName\":\"船员管理系统\",\"url\":\"http://cmp.msa.gov.cn/crew_mgt/pub/ssoLogin.action\",\"systemHeight\":\"1000\",\"systemWidth\":\"1200\",\"isUpdate\":\"是\",\"isDefaultRole\":\"否\",\"roleType\":\"一般角色\",\"accessPlatform\":\"p\",\"suitRange\":\"全国\",\"isBlank\":\"否\"},{\"businessType\":\"船员管理\",\"roleCode\":\"CMP_047_R00006\",\"productRoleId\":\"CMP_047_R00006\",\"roleName\":\"政务受理和发放\",\"appId\":\"047\",\"appName\":\"船员管理系统\",\"url\":\"http://cmp.msa.gov.cn/crew_mgt/pub/ssoLogin.action\",\"systemHeight\":\"1000\",\"systemWidth\":\"1200\",\"isUpdate\":\"是\",\"isDefaultRole\":\"否\",\"roleType\":\"一般角色\",\"accessPlatform\":\"p\",\"suitRange\":\"全国\",\"isBlank\":\"否\"},{\"businessType\":\"船员管理\",\"roleCode\":\"CMP_047_R00007\",\"productRoleId\":\"CMP_047_R00007\",\"roleName\":\"证书审核\",\"appId\":\"047\",\"appName\":\"船员管理系统\",\"url\":\"http://cmp.msa.gov.cn/crew_mgt/pub/ssoLogin.action\",\"systemHeight\":\"1000\",\"systemWidth\":\"1200\",\"isUpdate\":\"是\",\"isDefaultRole\":\"否\",\"roleType\":\"一般角色\",\"accessPlatform\":\"p\",\"suitRange\":\"全国\",\"isBlank\":\"否\"},{\"businessType\":\"船员管理\",\"roleCode\":\"CMP_047_R00013\",\"productRoleId\":\"CMP_047_R00013\",\"roleName\":\"船员信息管理\",\"appId\":\"047\",\"appName\":\"船员管理系统\",\"url\":\"http://cmp.msa.gov.cn/crew_mgt/pub/ssoLogin.action\",\"systemHeight\":\"1000\",\"systemWidth\":\"1200\",\"isUpdate\":\"是\",\"isDefaultRole\":\"否\",\"roleType\":\"一般角色\",\"accessPlatform\":\"p\",\"suitRange\":\"全国\",\"isBlank\":\"否\"},{\"businessType\":\"船舶管理\",\"roleCode\":\"CMP_059_R00001\",\"productRoleId\":\"00000000004\",\"roleName\":\"船舶进出口岸管理角色\",\"appId\":\"059\",\"appName\":\"船舶安全监督管理系统\",\"url\":\"http://cmp.msa.gov.cn/supervision/sys/ssoLogin.action\",\"systemHeight\":\"1000\",\"systemWidth\":\"1200\",\"isUpdate\":\"否\",\"isDefaultRole\":\"否\",\"roleType\":\"一般角色\",\"accessPlatform\":\"p\",\"suitRange\":\"全国\",\"isBlank\":\"否\"},{\"businessType\":\"船舶管理\",\"roleCode\":\"CMP_059_R00009\",\"productRoleId\":\"00000000011\",\"roleName\":\"船舶基本信息管理角色\",\"appId\":\"059\",\"appName\":\"船舶安全监督管理系统\",\"url\":\"http://cmp.msa.gov.cn/supervision/sys/ssoLogin.action\",\"systemHeight\":\"1000\",\"systemWidth\":\"1200\",\"isUpdate\":\"否\",\"isDefaultRole\":\"否\",\"roleType\":\"一般角色\",\"accessPlatform\":\"p\",\"suitRange\":\"全国\",\"isBlank\":\"否\"},{\"businessType\":\"船舶管理\",\"roleCode\":\"CMP_059_R00011\",\"productRoleId\":\"00000000016\",\"roleName\":\"统计分析角色（部局，地方局）\",\"appId\":\"059\",\"appName\":\"船舶安全监督管理系统\",\"url\":\"http://cmp.msa.gov.cn/supervision/sys/ssoLogin.action\",\"systemHeight\":\"1000\",\"systemWidth\":\"1200\",\"isUpdate\":\"否\",\"isDefaultRole\":\"否\",\"roleType\":\"一般角色\",\"accessPlatform\":\"p\",\"suitRange\":\"全国\",\"isBlank\":\"否\"},{\"businessType\":\"船舶管理\",\"roleCode\":\"CMP_059_R00017\",\"productRoleId\":\"00000000025\",\"roleName\":\"综合查询角色\",\"appId\":\"059\",\"appName\":\"船舶安全监督管理系统\",\"url\":\"http://cmp.msa.gov.cn/supervision/sys/ssoLogin.action\",\"systemHeight\":\"1000\",\"systemWidth\":\"1200\",\"isUpdate\":\"否\",\"isDefaultRole\":\"否\",\"roleType\":\"一般角色\",\"accessPlatform\":\"p\",\"suitRange\":\"全国\",\"isBlank\":\"否\"},{\"businessType\":\"行政综合\",\"roleCode\":\"CMP_071_R00004\",\"productRoleId\":\"CMP_071_R00004\",\"roleName\":\"普通用户\",\"appId\":\"071\",\"appName\":\"海事教育培训网\",\"url\":\"http://cmp.msa.gov.cn/Study/MSA/Portal/Login.aspx\",\"systemHeight\":\"1000\",\"systemWidth\":\"1200\",\"isUpdate\":\"否\",\"isDefaultRole\":\"是\",\"roleType\":\"一般角色\",\"accessPlatform\":\"p\",\"suitRange\":\"全国\",\"isBlank\":\"否\"},{\"businessType\":\"法制管理\",\"roleCode\":\"CMP_074_R00011\",\"productRoleId\":\"18.0\",\"roleName\":\"海事执法证-普通用户\",\"appId\":\"074\",\"appName\":\"海事执法资格管理\",\"url\":\"http://cmp.msa.gov.cn/HsZfzAppSm2/login/index.shtml\",\"systemHeight\":\"1000\",\"systemWidth\":\"1200\",\"isUpdate\":\"否\",\"isDefaultRole\":\"否\",\"roleType\":\"一般角色\",\"accessPlatform\":\"p\",\"suitRange\":\"全国\",\"isBlank\":\"否\"},{\"businessType\":\"船员管理\",\"roleCode\":\"CMP_047_R00037\",\"productRoleId\":\"CMP_047_R00037\",\"roleName\":\"企业信息确认\",\"appId\":\"047\",\"appName\":\"船员管理系统\",\"url\":\"http://cmp.msa.gov.cn/crew_mgt/pub/ssoLogin.action\",\"systemHeight\":\"1000\",\"systemWidth\":\"1200\",\"isUpdate\":\"是\",\"isDefaultRole\":\"否\",\"roleType\":\"一般角色\",\"accessPlatform\":\"p\",\"suitRange\":\"全国\",\"isBlank\":\"否\"},{\"businessType\":\"危防管理\",\"roleCode\":\"CMP_057_R00002\",\"productRoleId\":\"RI000000002\",\"roleName\":\"海事危防业务人员\",\"appId\":\"057\",\"appName\":\"海事危防管理系统\",\"url\":\"http://cmp.msa.gov.cn/dgpp/sso/cmp-system\",\"systemHeight\":\"1000\",\"systemWidth\":\"1200\",\"isUpdate\":\"否\",\"isDefaultRole\":\"否\",\"roleType\":\"一般角色\",\"accessPlatform\":\"p\",\"suitRange\":\"全国\",\"isBlank\":\"否\"},{\"businessType\":\"行政综合\",\"roleCode\":\"CMP_038_R00001\",\"productRoleId\":\"CMP_038_R00001\",\"roleName\":\"业务受理\",\"appId\":\"038\",\"appName\":\"综合政务\",\"url\":\"http://cmp.msa.gov.cn/gahCMP/\",\"systemHeight\":\"1000\",\"systemWidth\":\"1200\",\"isUpdate\":\"否\",\"isDefaultRole\":\"否\",\"roleType\":\"一般角色\",\"accessPlatform\":\"p\",\"suitRange\":\"全国\",\"isBlank\":\"否\"},{\"businessType\":\"行政综合\",\"roleCode\":\"CMP_038_R00007\",\"productRoleId\":\"CMP_038_R00007\",\"roleName\":\"发证\",\"appId\":\"038\",\"appName\":\"综合政务\",\"url\":\"http://cmp.msa.gov.cn/gahCMP/\",\"systemHeight\":\"1000\",\"systemWidth\":\"1200\",\"isUpdate\":\"否\",\"isDefaultRole\":\"否\",\"roleType\":\"一般角色\",\"accessPlatform\":\"p\",\"suitRange\":\"全国\",\"isBlank\":\"否\"},{\"businessType\":\"行政综合\",\"roleCode\":\"CMP_038_R00008\",\"productRoleId\":\"CMP_038_R00008\",\"roleName\":\"归档\",\"appId\":\"038\",\"appName\":\"综合政务\",\"url\":\"http://cmp.msa.gov.cn/gahCMP/\",\"systemHeight\":\"1000\",\"systemWidth\":\"1200\",\"isUpdate\":\"否\",\"isDefaultRole\":\"否\",\"roleType\":\"一般角色\",\"accessPlatform\":\"p\",\"suitRange\":\"全国\",\"isBlank\":\"否\"}]}]}}}}");
    	//包沐曦
//    	JSONObject jsonResult = JSONObject.fromObject("{\"result\":\"1\",\"info\":\"查询成功\",\"datas\":{\"table\":{\"tableName\":\"user\",\"row\":{\"city\":\"南京市\",\"cn\":\"包沐曦\",\"country\":\"中国\",\"credentialsType\":\"身份证\",\"departments\":\"06010040\",\"email\":\"baomuxi@msa.gov.cn\",\"employeeType\":\"正式用户\",\"gender\":\"女\",\"givenName\":\"包沐曦\",\"idNumber\":\"320106199005100828\",\"ismanager\":\"否\",\"mail\":\"baomuxi@msa.gov.cn\",\"mobile\":\"13921446258\",\"orderNum\":\"-1\",\"orgName\":\"南京海事局\",\"organizations\":\"0601\",\"province\":\"江苏省\",\"sn\":\"包沐曦\",\"uid\":\"320106199005100828\",\"userName\":\"包沐曦\",\"userStatus\":\"Active\",\"userType\":\"正式用户\",\"workType\":\"公务员编制\",\"table\":[{\"tableName\":\"role\",\"row\":[{\"businessType\":\"行政综合\",\"roleCode\":\"CMP_99_R00001\",\"productRoleId\":\"1\",\"roleName\":\"门户基础角色\",\"appId\":\"99\",\"appName\":\"公用\",\"systemHeight\":\"1000\",\"systemWidth\":\"1200\",\"isUpdate\":\"否\",\"isDefaultRole\":\"是\",\"roleType\":\"通用角色\",\"accessPlatform\":\"p\",\"suitRange\":\"全国\"},{\"businessType\":\"系统管理\",\"roleCode\":\"CMP_006_R00008\",\"productRoleId\":\"8\",\"roleName\":\"默认角色\",\"appId\":\"006\",\"appName\":\"运维系统\",\"url\":\"http://cmp.msa.gov.cn/cas\",\"systemHeight\":\"1000\",\"systemWidth\":\"1200\",\"isUpdate\":\"否\",\"isDefaultRole\":\"是\",\"roleType\":\"默认角色\",\"logoutUrl\":\"http://198.10.9.207/cas/idmlogout\",\"accessPlatform\":\"p\",\"suitRange\":\"全国\",\"isBlank\":\"否\"},{\"businessType\":\"行政综合\",\"roleCode\":\"CMP_010_R00004\",\"productRoleId\":\"4\",\"roleName\":\"公告发布者\",\"appId\":\"010\",\"appName\":\"统一内容发布系统\",\"url\":\"http://cmp.msa.gov.cn/URCms/jeeadmin/jeecms/index.do\",\"systemHeight\":\"1000\",\"systemWidth\":\"1200\",\"isUpdate\":\"否\",\"isDefaultRole\":\"是\",\"roleType\":\"默认角色\",\"logoutUrl\":\"http://cmp.msa.gov.cn/URCms/jeeadmin/jeecms/logout.do?returnUrl=index.do\",\"accessPlatform\":\"p\",\"suitRange\":\"全国\",\"isBlank\":\"否\"},{\"businessType\":\"行政综合\",\"roleCode\":\"CMP_015_R0021\",\"productRoleId\":\"20150913000228\",\"roleName\":\"航行通警告录入\",\"appId\":\"015\",\"appName\":\"部海事局门户网站\",\"url\":\"http://cmp.msa.gov.cn/msacncms1/admin\",\"systemHeight\":\"1000\",\"systemWidth\":\"1200\",\"isUpdate\":\"否\",\"isDefaultRole\":\"否\",\"roleType\":\"一般角色\",\"logoutUrl\":\"http://198.10.10.17/msacncms1/admin/logout.do?event=Logout\",\"accessPlatform\":\"p\",\"suitRange\":\"全国\",\"isBlank\":\"否\"},{\"businessType\":\"行政综合\",\"roleCode\":\"CMP_015_R0068\",\"productRoleId\":\"1C85B8C0-00F0-45A9-962D-7B5B54085E3A\",\"roleName\":\"航行通警告录入员\",\"appId\":\"015\",\"appName\":\"部海事局门户网站\",\"url\":\"http://cmp.msa.gov.cn/msacncms1/admin\",\"systemHeight\":\"1000\",\"systemWidth\":\"1200\",\"isUpdate\":\"否\",\"isDefaultRole\":\"否\",\"roleType\":\"一般角色\",\"logoutUrl\":\"http://198.10.10.17/msacncms1/admin/logout.do?event=Logout\",\"accessPlatform\":\"p\",\"suitRange\":\"全国\",\"isBlank\":\"否\"},{\"businessType\":\"行政综合\",\"roleCode\":\"CMP_015_R0077\",\"productRoleId\":\"CMP_015_R0077\",\"roleName\":\"航行通警告管理\",\"appId\":\"015\",\"appName\":\"部海事局门户网站\",\"url\":\"http://cmp.msa.gov.cn/msacncms1/admin\",\"systemHeight\":\"1000\",\"systemWidth\":\"1200\",\"isUpdate\":\"否\",\"isDefaultRole\":\"否\",\"roleType\":\"一般角色\",\"logoutUrl\":\"http://198.10.10.17/msacncms1/admin/logout.do?event=Logout\",\"accessPlatform\":\"p\",\"suitRange\":\"全国\",\"isBlank\":\"否\"},{\"businessType\":\"行政综合\",\"roleCode\":\"CMP_022_R00001\",\"productRoleId\":\"CMP_022_R00001\",\"roleName\":\"内网邮件\",\"appId\":\"022\",\"appName\":\"内网邮件\",\"url\":\"http://cmp.msa.gov.cn/coremail/sso.jsp?to=webmail\",\"systemHeight\":\"1000\",\"systemWidth\":\"1200\",\"isUpdate\":\"是\",\"isDefaultRole\":\"是\",\"roleType\":\"默认角色\",\"accessPlatform\":\"p\",\"suitRange\":\"全国\",\"isBlank\":\"是\"},{\"businessType\":\"系统管理\",\"roleCode\":\"CMP_035_R000002\",\"productRoleId\":\"CMP_035_R000002\",\"roleName\":\"MDM系统用户\",\"appId\":\"035\",\"appName\":\"移动安全管理系统\",\"url\":\"http://cmp.msa.gov.cn/webservice/haishiju/webLogin.php\",\"systemHeight\":\"1000\",\"systemWidth\":\"1200\",\"isUpdate\":\"否\",\"isDefaultRole\":\"是\",\"roleType\":\"默认角色\",\"accessPlatform\":\"p\",\"suitRange\":\"全国\",\"isBlank\":\"是\"},{\"businessType\":\"船员管理\",\"roleCode\":\"CMP_047_R00001\",\"productRoleId\":\"CMP_047_R00001\",\"roleName\":\"一般成员\",\"appId\":\"047\",\"appName\":\"船员管理系统\",\"url\":\"http://cmp.msa.gov.cn/crew_mgt/pub/ssoLogin.action\",\"systemHeight\":\"1000\",\"systemWidth\":\"1200\",\"isUpdate\":\"是\",\"isDefaultRole\":\"否\",\"roleType\":\"一般角色\",\"accessPlatform\":\"p\",\"suitRange\":\"全国\",\"isBlank\":\"否\"},{\"businessType\":\"船员管理\",\"roleCode\":\"CMP_047_R00006\",\"productRoleId\":\"CMP_047_R00006\",\"roleName\":\"政务受理和发放\",\"appId\":\"047\",\"appName\":\"船员管理系统\",\"url\":\"http://cmp.msa.gov.cn/crew_mgt/pub/ssoLogin.action\",\"systemHeight\":\"1000\",\"systemWidth\":\"1200\",\"isUpdate\":\"是\",\"isDefaultRole\":\"否\",\"roleType\":\"一般角色\",\"accessPlatform\":\"p\",\"suitRange\":\"全国\",\"isBlank\":\"否\"},{\"businessType\":\"船员管理\",\"roleCode\":\"CMP_047_R00013\",\"productRoleId\":\"CMP_047_R00013\",\"roleName\":\"船员信息管理\",\"appId\":\"047\",\"appName\":\"船员管理系统\",\"url\":\"http://cmp.msa.gov.cn/crew_mgt/pub/ssoLogin.action\",\"systemHeight\":\"1000\",\"systemWidth\":\"1200\",\"isUpdate\":\"是\",\"isDefaultRole\":\"否\",\"roleType\":\"一般角色\",\"accessPlatform\":\"p\",\"suitRange\":\"全国\",\"isBlank\":\"否\"},{\"businessType\":\"船舶管理\",\"roleCode\":\"CMP_059_R00009\",\"productRoleId\":\"00000000011\",\"roleName\":\"船舶基本信息管理角色\",\"appId\":\"059\",\"appName\":\"船舶安全监督管理系统\",\"url\":\"http://cmp.msa.gov.cn/supervision/sys/ssoLogin.action\",\"systemHeight\":\"1000\",\"systemWidth\":\"1200\",\"isUpdate\":\"否\",\"isDefaultRole\":\"否\",\"roleType\":\"一般角色\",\"accessPlatform\":\"p\",\"suitRange\":\"全国\",\"isBlank\":\"否\"},{\"businessType\":\"行政综合\",\"roleCode\":\"CMP_071_R00004\",\"productRoleId\":\"CMP_071_R00004\",\"roleName\":\"普通用户\",\"appId\":\"071\",\"appName\":\"海事教育培训网\",\"url\":\"http://cmp.msa.gov.cn/Study/MSA/Portal/Login.aspx\",\"systemHeight\":\"1000\",\"systemWidth\":\"1200\",\"isUpdate\":\"否\",\"isDefaultRole\":\"是\",\"roleType\":\"一般角色\",\"accessPlatform\":\"p\",\"suitRange\":\"全国\",\"isBlank\":\"否\"},{\"businessType\":\"危防管理\",\"roleCode\":\"CMP_057_R00002\",\"productRoleId\":\"RI000000002\",\"roleName\":\"海事危防业务人员\",\"appId\":\"057\",\"appName\":\"海事危防管理系统\",\"url\":\"http://cmp.msa.gov.cn/dgpp/sso/cmp-system\",\"systemHeight\":\"1000\",\"systemWidth\":\"1200\",\"isUpdate\":\"否\",\"isDefaultRole\":\"否\",\"roleType\":\"一般角色\",\"accessPlatform\":\"p\",\"suitRange\":\"全国\",\"isBlank\":\"否\"},{\"businessType\":\"行政综合\",\"roleCode\":\"CMP_038_R00002\",\"productRoleId\":\"CMP_038_R00002\",\"roleName\":\"业务初审\",\"appId\":\"038\",\"appName\":\"综合政务\",\"url\":\"http://cmp.msa.gov.cn/gahCMP/\",\"systemHeight\":\"1000\",\"systemWidth\":\"1200\",\"isUpdate\":\"否\",\"isDefaultRole\":\"否\",\"roleType\":\"一般角色\",\"accessPlatform\":\"p\",\"suitRange\":\"全国\",\"isBlank\":\"否\"},{\"businessType\":\"行政综合\",\"roleCode\":\"CMP_038_R00003\",\"productRoleId\":\"CMP_038_R00003\",\"roleName\":\"业务复审\",\"appId\":\"038\",\"appName\":\"综合政务\",\"url\":\"http://cmp.msa.gov.cn/gahCMP/\",\"systemHeight\":\"1000\",\"systemWidth\":\"1200\",\"isUpdate\":\"否\",\"isDefaultRole\":\"否\",\"roleType\":\"一般角色\",\"accessPlatform\":\"p\",\"suitRange\":\"全国\",\"isBlank\":\"否\"},{\"businessType\":\"行政综合\",\"roleCode\":\"CMP_038_R00006\",\"productRoleId\":\"CMP_038_R00006\",\"roleName\":\"制证\",\"appId\":\"038\",\"appName\":\"综合政务\",\"url\":\"http://cmp.msa.gov.cn/gahCMP/\",\"systemHeight\":\"1000\",\"systemWidth\":\"1200\",\"isUpdate\":\"否\",\"isDefaultRole\":\"否\",\"roleType\":\"一般角色\",\"accessPlatform\":\"p\",\"suitRange\":\"全国\",\"isBlank\":\"否\"}]}]}}}}");
    	


    	if(jsonResult.getInt("result") == 1 && jsonResult.getJSONObject("datas").getJSONObject("table").has("row")){
    		JSONObject jsonUser = jsonResult.getJSONObject("datas").getJSONObject("table").getJSONObject("row");
    		
    		userDetail.setUserid(jsonUser.getString("idNumber"));
    		userDetail.setUsername(jsonUser.getString("userName"));
    		userDetail.setMobile(jsonUser.optString("mobile"));
    		
    		ctx = new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"}); 
    	    cn.gov.msa.service.OrgService orgService = (cn.gov.msa.service.OrgService)ctx.getBean("OrgService"); 
    	    Org org  =orgService.selectBy4aOrgCode(jsonUser.getString("organizations"));
    	    if(null!=org)
    	    	userDetail.setOrgcode(org.getOrgCode());
    	    else
    	    	userDetail.setOrgcode(jsonUser.getString("organizations"));
    		
    		userDetail.setOrgcode4a(jsonUser.getString("organizations"));
    		if(jsonUser.has("orgName"))
    			userDetail.setOrgname(jsonUser.getString("orgName"));
    		if(jsonUser.has("departments"))
    			userDetail.setDeptcode(jsonUser.getString("departments"));
    		if(jsonUser.has("table")) {
    			JSONArray tables = jsonUser.getJSONArray("table");
    			for (int i = 0; i < tables.size(); i++) {
					JSONObject table = tables.getJSONObject(i);
					if("role".equals(table.getString("tableName"))) {
						JSONArray rows = table.getJSONArray("row");
						List<Role> roles = new ArrayList<Role>();
						for (int j = 0; j < rows.size(); j++) {
							JSONObject row = rows.getJSONObject(j);
							
							if(row.optString("roleCode").indexOf("_038_") > 0){
								Role role = new Role();
								role.setRolecode(row.optString("roleCode"));
								role.setRolename(row.optString("roleName"));
								roles.add(role);
							}
						}
						userDetail.setRoles(roles);
					}
				}
    		}
    	}
		return userDetail;
	}

}
